﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DataAccess;
using DataAccess.DBConnections;
using DataAccess.ProxyConnections;
using Microsoft.VisualBasic;
using Oracle.DataAccess.Client;
using Web.Core;

namespace Web.Business
{
    public partial class CompanyBal
    {
        #region | Conts |
        
        private static DBConnectionsEngine dbEngine;

        private static OracleConnection Conn;

        private static Hashtable hashtable;

        private static CompanyBal _dal;

        public static object result = null;

        private static System.String sql = "";

        public static String err = null;

        public static CompanyBal GetInstance()
        {
            /* for Init */

            dbEngine = new DBConnectionsEngine();

            Conn = new DatabaseProxy().GetAuthConnection();

            _dal = new CompanyBal();

            return _dal;
        }

        #endregion

        #region | dbEngine |

        public IEnumerable<p_COMPANY> Select(System.Web.UI.Page page, p_COMPANY obj)
        {
            IEnumerable<p_COMPANY> entities = new List<p_COMPANY>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (obj != null)
                {
                    if (!String.IsNullOrEmpty(obj.OU_CODE))
                    {
                        concast.Append(" AND OU_CODE=:OU_CODE ");
                        hashtable.Add(":OU_CODE", obj.OU_CODE);
                    }

                    if (!String.IsNullOrEmpty(obj.COMPANY_CODE))
                    {
                        concast.Append(" AND COMPANY_CODE=:COMPANY_CODE ");
                        hashtable.Add(":COMPANY_CODE", obj.COMPANY_CODE);
                    }

                    if (!String.IsNullOrEmpty(obj.COMPANY_NAME_EN))
                    {
                        concast.Append(" AND COMPANY_NAME_EN=:COMPANY_NAME_EN ");
                        hashtable.Add(":COMPANY_NAME_EN", obj.COMPANY_NAME_EN);
                    }

                    if (!String.IsNullOrEmpty(obj.COMPANY_NAME_TH))
                    {
                        concast.Append(" AND COMPANY_NAME_TH=:COMPANY_NAME_TH ");
                        hashtable.Add(":COMPANY_NAME_TH", obj.COMPANY_NAME_TH);
                    }

                    if (!String.IsNullOrEmpty(obj.ADDR1))
                    {
                        concast.Append(" AND ADDR1=:ADDR1 ");
                        hashtable.Add(":ADDR1", obj.ADDR1);
                    }

                    if (!String.IsNullOrEmpty(obj.ADDR2))
                    {
                        concast.Append(" AND ADDR2=:ADDR2 ");
                        hashtable.Add(":ADDR2", obj.ADDR2);
                    }

                    if (!String.IsNullOrEmpty(obj.ADDR3))
                    {
                        concast.Append(" AND ADDR3=:ADDR3 ");
                        hashtable.Add(":ADDR3", obj.ADDR3);
                    }

                    if (!String.IsNullOrEmpty(obj.CITY))
                    {
                        concast.Append(" AND CITY=:CITY ");
                        hashtable.Add(":CITY", obj.CITY);
                    }

                    if (!String.IsNullOrEmpty(obj.PSTSD))
                    {
                        concast.Append(" AND PSTSD=:PSTSD ");
                        hashtable.Add(":PSTSD", obj.PSTSD);
                    }

                    if (!String.IsNullOrEmpty(obj.TELNO))
                    {
                        concast.Append(" AND TELNO=:TELNO ");
                        hashtable.Add(":TELNO", obj.TELNO);
                    }

                    if (!String.IsNullOrEmpty(obj.FAXNO))
                    {
                        concast.Append(" AND FAXNO=:FAXNO ");
                        hashtable.Add(":FAXNO", obj.FAXNO);
                    }

                    if (!Information.IsNothing(obj.LOGO))
                    {
                        concast.Append(" AND LOGO=:LOGO ");
                        hashtable.Add(":LOGO", obj.LOGO);
                    }

                    if (!String.IsNullOrEmpty(obj.AUTH_LDAP))
                    {
                        concast.Append(" AND AUTH_LDAP=:AUTH_LDAP ");
                        hashtable.Add(":AUTH_LDAP", obj.AUTH_LDAP);
                    }

                    if (!String.IsNullOrEmpty(obj.AUTH_USERNAME))
                    {
                        concast.Append(" AND AUTH_USERNAME=:AUTH_USERNAME ");
                        hashtable.Add(":AUTH_USERNAME", obj.AUTH_USERNAME);
                    }

                    if (!String.IsNullOrEmpty(obj.AUTH_PASSWORD))
                    {
                        concast.Append(" AND AUTH_PASSWORD=:AUTH_PASSWORD ");
                        hashtable.Add(":AUTH_PASSWORD", obj.AUTH_PASSWORD);
                    }

                    if (!String.IsNullOrEmpty(obj.USE_FLAG))
                    {
                        concast.Append(" AND USE_FLAG=:USE_FLAG ");
                        hashtable.Add(":USE_FLAG", obj.USE_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.ACTIVE_FLAG))
                    {
                        concast.Append(" AND ACTIVE_FLAG=:ACTIVE_FLAG ");
                        hashtable.Add(":ACTIVE_FLAG", obj.ACTIVE_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.COMPANY_NAME_ABBR))
                    {
                        concast.Append(" AND COMPANY_NAME_ABBR=:COMPANY_NAME_ABBR ");
                        hashtable.Add(":COMPANY_NAME_ABBR", obj.COMPANY_NAME_ABBR);
                    }

                }
                sql = System.String.Format(@" SELECT OU_CODE ,COMPANY_CODE ,COMPANY_NAME_EN ,COMPANY_NAME_TH ,ADDR1 ,ADDR2 ,ADDR3 ,CITY ,PSTSD ,TELNO ,FAXNO ,LOGO ,AUTH_LDAP ,AUTH_USERNAME ,AUTH_PASSWORD ,USE_FLAG ,ACTIVE_FLAG ,COMPANY_NAME_ABBR  FROM COMPANY");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<p_COMPANY>(sql, hashtable, Conn).AsParallel();

            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                     page: page,
                     message: Core.Exception.ModalException(
                                         method: System.Reflection.MethodBase.GetCurrentMethod(),
                                         ex: ex));
                entities = null;
            }
            return entities;
        }

        #endregion

    }
}